package xuan.mall.system.infrastructure.security.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author: rongxuanzhou
 * @create: 2025/6/1
 * @Description:
 */
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class MallSecurityConfig {

    private final CorsConfig corsConfig;
    private final AuthenticationConfig authConfig;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf(CsrfConfigurer::disable)
                .authorizeHttpRequests(req -> {
                    req
                            .requestMatchers("/login").permitAll()
                            .anyRequest().authenticated();
                });
        // 处理跨域
        corsConfig.configure(http);
        // 认证模块
        authConfig.configure(http);
        return http.build();
    }
}
